package com.meevle.web.tags;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.meevle.business.UserDelegate;
import com.meevle.vo.UserVO;

public class CheckAvailabilityUserTag extends SimpleTagSupport{

	private String username;
	private UserDelegate object;
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public UserDelegate getObject() {
		return object;
	}
	
	public void setObject(UserDelegate object) {
		this.object = object;
	}
	
	
	public void doTag()
	{
		
		boolean b = false;
		try {
			UserDelegate.getUser(new UserVO(username));
		} catch (IllegalArgumentException e) {
			;
			b = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JspContext context = getJspContext();
		if(b)
		{
			context.setAttribute("availableMessage", "This username is available!");
		}
		else
			context.setAttribute("availableMessage", "This username is NOT available!");
		
		context.setAttribute("isAvailable", b);
		
        JspFragment body = getJspBody();
        try {
			body.invoke(null);
		} catch (JspException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			;
		}

	}
}
